let express = require('express');
let crypto = require('crypto');
let router = express.Router();
let filter = require('../../components/filter');

/**
 * 查询角色数据 by p 2018-06-11
 *
 * 按条件查询角色数据
 *
 */
router.get('/', filter.authorize, function (req, res, next) {
    let params = req.query;

    var limits = "";
    var sql = "";
    var datas = [];

    req.app.db.query("select count(1) as totalCount from role " + sql, datas, function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results[0].totalCount === 0) {
            res.json({"success": true, "data": {}, "totalCount": 0});
            return;
        }

        let totalCount = results[0].totalCount;

        if (params.pageNo > 0 && params.pageSize > 0) {
            limits = " limit ?, ?";
            datas.push((params.pageNo - 1) * params.pageSize);
            datas.push(params.pageSize * 1);
        }

        req.app.db.query("select * from role " + limits, datas, function (err, results, fields) {
            if (err) {
                res.json({"success": false, "message": global.databaseErr, "detail": err});
                return;
            }

            res.json({"success": true, "data": results, "totalCount": totalCount, "message": ""});
        });
    });
});

/**
 * 查看角色数据 by p 2018-06-11
 *
 * 按id查看指定的角色信息
 */
router.get('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query("select * from role where id = ?", [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results.length === 0) {
            res.json({"success": false, "message": global.noDataErr});
            return;
        }

        res.json({"success": true, "data": results[0]});
    });
});

/**
 * 修改角色数据 by p 2018-06-11
 *
 *
 */
router.put('/:id', filter.authorize, function (req, res, next) {
    let params = req.body;

    if (params.id === undefined || params.id === '') {
        res.json({"success": false, "message": "角色编码错误！"});
        return;
    }

    if (params.name === undefined || params.name === "") {
        res.json({"success": false, "message": "角色名称不能为空！"});
        return;
    }

    if (params.power === undefined || params.power === "") {
        res.json({"success": false, "message": "角色权限不能为空！"});
        return;
    }

    if (params.defaultPage === undefined || params.defaultPage === "") {
        res.json({"success": false, "message": "角色默认页面不能为空！"});
        return;
    }

    req.app.db.query("update role set name = ?, power = ?, defaultPage = ? where id = ?", [params.name, params.power, params.defaultPage, params.id], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.putMsg});
    });

});

/**
 * 新增角色数据 by p 2018-06-11
 *
 *
 */
router.post('/', filter.authorize, function (req, res, next) {
    let params = req.body;
    if (params.name === undefined || params.name === "") {
        res.json({"success": false, "message": "角色名称不能为空！"});
        return;
    }

    if (params.power === undefined || params.power === "") {
        res.json({"success": false, "message": "角色权限不能为空！"});
        return;
    }

    if (params.defaultPage === undefined || params.defaultPage === "") {
        res.json({"success": false, "message": "角色默认页面不能为空！"});
        return;
    }

    req.app.db.query("insert into role(name, power, defaultPage) values(?, ?, ?)", [params.name, params.power, params.defaultPage], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.postMsg});
    });
});

/**
 * 删除角色信息 by v 2018-06-11
 *
 * 按id删除指定的角色信息
 */
router.delete('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query('delete from role where id = ?', [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.deleteMsg});
    });
});

module.exports = router;